package com.example.mymvvmdemo.models.my_article.adapter;

import android.content.Context;
import android.view.View;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseVBAdapter;
import com.example.mymvvmdemo.databinding.ItemHomeBinding;
import com.example.mymvvmdemo.databinding.ItemMyArticleBinding;
import com.example.mymvvmdemo.models.main.fragment.home.entity.BlogArticlesEntity;
import com.example.mymvvmdemo.utils.GlideUtils;
import com.example.mymvvmdemo.utils.StrUtils;

import java.util.ArrayList;

public class MyArticleAdapter extends BaseVBAdapter<BlogArticlesEntity.DataDTO, ItemMyArticleBinding> {

    private ArticleListener listener;

    public void setListener(ArticleListener listener) {
        this.listener = listener;
    }

    /**
     * @param context
     */
    public MyArticleAdapter(Context context, ArrayList<BlogArticlesEntity.DataDTO> list) {
        super(context);
        setDataList(list);
    }

    @Override
    protected void updateData(ItemMyArticleBinding bind, BlogArticlesEntity.DataDTO item, int position) {

        if (item == null) {
            return;
        }

        boolean edit = item.isEdit();
        bind.cbSelect.setVisibility(edit ? View.VISIBLE : View.GONE);

        boolean select = item.isSelect();
        bind.cbSelect.setChecked(select);

        String title = item.getTitle();
        if (!StrUtils.isEmpty(title)) {
            bind.tvTitle.setText(title);
        }


        String desc = item.getContent();
        if (!StrUtils.isEmpty(desc)) {
            bind.tvDesc.setText(desc);
        }

        String time = item.getCreated_at();
        if (!StrUtils.isEmpty(time)) {
            bind.tvTime.setText(time);
        }

        String imageUrl = item.getImage_url();
        GlideUtils.loadImage(context, imageUrl, R.mipmap.app_icon, bind.ivHome);


        bind.cbSelect.setOnClickListener(view -> {
            if (listener != null) {
                item.setSelect(bind.cbSelect.isChecked());
                listener.onMyArticleClick(position);
            }
        });

    }

    public void changeEditModel(boolean isEdit) {
        for (int i = 0; i < dataList.size(); i++) {
            dataList.get(i).setEdit(isEdit);
        }
        notifyDataSetChanged();
        if (!isEdit) {
            for (int i = 0; i < dataList.size(); i++) {
                dataList.get(i).setSelect(false);
            }
        }
    }

    public interface ArticleListener {
        void onMyArticleClick(int position);
    }

}
